﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace SAF.Controls
{
    [ToolboxItem(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public partial class NewReportForm : XtraForm
    {
        IList<sysReport> sysReports = null;
        private NewReportForm(IList<sysReport> sysReports, Form topLevelForm)
        {
            InitializeComponent();
            this.sysReports = sysReports;
            for (int i = 0; i < sysReports.Count + 1; i++)
            {
                this.cbxSortIndex.Properties.Items.Add(i + 1);
            }
            this.cbxSortIndex.EditValue = sysReports.Count + 1;

            this.StartPosition = FormStartPosition.CenterScreen;
            if (topLevelForm != null)
            {
                this.Owner = topLevelForm;
                this.StartPosition = FormStartPosition.CenterParent;
            }
        }

        internal static bool NewReport(IList<sysReport> sysReports, ref sysReport newReport, Form topLevelForm)
        {
            using (NewReportForm inputbox = new NewReportForm(sysReports, topLevelForm))
            {
                if (inputbox.ShowDialog() == DialogResult.OK)
                {
                    if (newReport == null)
                        newReport = new sysReport();
                    newReport.Iden = Guid.NewGuid();
                    newReport.ReportFormatName = inputbox.txtReportFormatName.Text.Trim();


                    newReport.IsActive = true;
                    newReport.IsDefault = inputbox.chkIsDefault.Checked;
                    newReport.SortIndex = int.Parse(inputbox.cbxSortIndex.EditValue.ToString());
                    newReport.Remark = inputbox.txtRemark.Text;
                    if (newReport.SortIndex > sysReports.Count)
                    {
                        sysReports.Add(newReport);
                    }
                    else
                    {
                        sysReports.Insert(newReport.SortIndex - 1, newReport);
                        for (int i = 0; i < sysReports.Count; i++)
                        {
                            sysReports[i].SortIndex = i + 1;
                        }
                    }
                    return true;
                }
                else
                {
                    newReport = null;
                    return false;
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtReportFormatName.Text))
            {
                XtraMessageBox.Show("请输入报表名称！", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtReportFormatName.Focus();
                return;
            }

            if (this.sysReports.Any(x => x.ReportFormatName == this.txtReportFormatName.Text.Trim()))
            {
                XtraMessageBox.Show(string.Format("报表 \"{0}\" 已存在，报表名称不能相同！", this.txtReportFormatName.Text.Trim()));
                this.txtReportFormatName.Focus();
                return;
            }

            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
